<?php 
/**
 * loadLayout($layoutName, $params = array())
 * setGroupLayout($groupLayoutName)
 * getGroupLayout() 
 * 
 */
class __LAYOUT
{
	private static $_instance;
	public $layoutGroup = 'default';
	
	public static function getInstance() {
		if (!self::$_instance) self::$_instance = new self();

		return self::$_instance;
	}

	public function __construct() {
		$this->layoutGroup = $appSetup['layout'] ? $this->layoutGroup : 'default';
	}
	
	public function loadLayout($layoutName, $params = array()) {
		$layoutName = ucfirst(strtolower($layoutName));
		if (!file_exists(CORE_LAYOUT_DIR . '/' . $this->layoutGroup . '/layout/' . $layoutName . '.php')) {
			if (file_exists(CORE_LAYOUT_DIR . '/default/layout/' . $layoutName . '.php')) {
				$this->setGroupLayout('default');
			} else {
				// return error
				die('Not found: layout::' . $layoutName);
			}
		}

		ob_start();
		require (CORE_LAYOUT_DIR . '/' . $this->layoutGroup . '/layout/' . $layoutName . '.php');
		$contents = ob_get_contents();
		ob_end_clean();

		// Load sub layout
		while(preg_match('/\{yp\:load\s([^\}]+)\}/', $contents, $matches)) {
			$layoutLoaded = $this->loadLayout($matches[1], $params);
			if ($layoutLoaded) {
				$contents = str_replace($matches[0], $layoutLoaded, $contents);
			}
		}

		$this->_assignParams($contents, $params);
		$this->_assignDefaultParams($contents);

		return $contents;
	}
	
	public function load_sublayout_callback($layout) {
		
	}
	
	// Tam thoi nhu the nay di
	private function _assignParams(&$contents, $params = array()) {
		foreach ($params as $paramKey => $paramValue) {
			if (is_array($params["$paramKey"])) {
				foreach ($params["$paramKey"] as $paramKeySub => $paramValueSub) {
					$contents = preg_replace("/\{yp\:raw\s$paramKey\.$paramKeySub\}/", $paramValueSub, $contents);
				}
			} else {
				$contents = preg_replace("/\{yp\:raw\s$paramKey\}/", $paramValue, $contents);
			}
		}
		
		$this->_assignSystemParams($contents);
	}

	private function _assignDefaultParams(&$contents) {
		// Url 
		$url_params = array(
			'siteurl' => __APPLICATION::getInstance()->getPath(),
			'cssPath' => __APPLICATION::getInstance()->getPathCss(),
			'cssFullPath' => __APPLICATION::getInstance()->getFullPathCss(),
			'jsPath' => __APPLICATION::getInstance()->getPathJs(),
			'jsFullPath' => __APPLICATION::getInstance()->getFullPathJs(),
			'imgPath' => __APPLICATION::getInstance()->getPathImg(),
			'imgFullPath' => __APPLICATION::getInstance()->getFullPathImg(),
			'home' => __APPLICATION::getInstance()->getPath(),
			'about' => __APPLICATION::getInstance()->getPath() . '/?controller=about',
			'contact' => __APPLICATION::getInstance()->getPath() . '/?controller=contact',
			'news' => __APPLICATION::getInstance()->getPath() . '/?controller=news',
			'login' => __APPLICATION::getInstance()->getPath() . '/?controller=user&module=login',
			'logout' => __APPLICATION::getInstance()->getPath() . '/?controller=user&module=logout',
			'register' => __APPLICATION::getInstance()->getPath() . '/?controller=user&module=register',
			'admin' => __APPLICATION::getInstance()->getPath() . '/?controller=admin',
		);
		foreach ($url_params as $paramKey => $paramValue) {
			$contents = preg_replace("/\{yp\:url\s$paramKey\}/", $paramValue, $contents);
		}

		// Some var 
		$raws = array(
			'title' => 'Welcome to ypCore', 
		);
		foreach ($raws as $paramKey => $paramValue) {
			$contents = preg_replace("/\{yp\:raw\s$paramKey\}/", $paramValue, $contents);
		}

		// debug 
		$debugs = array(
			'debug' => array('time' => __DEBUGS::getPageLoadTime(START_TIME)),
			'message' => '',
		);
		$this->_assignParams($contents, $debugs);
	}

	private function _assignSystemParams(&$contents) {
		$params = $this->_getSystemParams();
		foreach ($params as $paramKey => $paramValue) {
			$contents = preg_replace("/\{yp\:system\s$paramKey\}/", $paramValue, $contents);
		}
	}

	private function _getSystemParams() {
		$params = array(
			'root' => __REQUEST::getInstance()->getSiteUrl(),
			'css_path' => __REQUEST::getInstance()->getSiteUrl() . '/layout/' . $this->layoutGroup . '/css',
			'js_path' => __REQUEST::getInstance()->getSiteUrl() . '/layout/' . $this->layoutGroup . '/js',
			'img_path' => __REQUEST::getInstance()->getSiteUrl() . '/layout/' . $this->layoutGroup . '/images',
			'layout_path' => __REQUEST::getInstance()->getSiteUrl() . '/layout/' . $this->layoutGroup,
		);

		return $params;
	}

	public function setGroupLayout($groupLayoutName) {
		$this->layoutGroup = $groupLayoutName;
	}
	
	public function getGroupLayout() {
		return $this->layoutGroup;
	}

}